import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StorageProvider } from '../../providers/storage/storage';
import { HttpserviesProvider } from '../../providers/httpservies/httpservies';
import { Http } from '@angular/http';


/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  public userinfo = {
    username:'',
    password:''
  }

  public history = '';

  constructor(public navCtrl: NavController, public navParams: NavParams,public httpServe:HttpserviesProvider,public storage:StorageProvider) {
    this.history=this.navParams.get('history');
    console.log(this.history);

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  //登录
  doLoginData(){
    console.log(this.userinfo);
    var api = 'api/doLogin';
    var _that = this;

    this.httpServe.doPostData(api,this.userinfo,function(data){
      console.log(data);

      if(data.success){
        //登录成功
        _that.storage.set('userinfo',data.userinfo[0]);

        if(_that.history=='order'){
          _that.navCtrl.pop();
        }else{
          _that.navCtrl.popToRoot();
        }

      }else{
        alert('登录失败，请稍后重试');
      }
    })
  }

}
